package usually;
//5. 最长回文子串
public class Num5_longestPalindrome {
    public String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        //保存结果
        String ret = "";
        for (int i = 0; i < s.length(); i++) {
            //奇回文
            int l = i , r = i ;
            while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r) ){
                l--;
                r++;
            }
            if (r - l - 1 > ret.length()){
                ret = s.substring(l + 1 , r);
            }
            //偶回文
             l = i - 1;r = i;
            while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r) ){
                l--;
                r++;
            }
            if (r - l - 1 > ret.length()){
                ret = s.substring(l + 1 , r);
            }
        }
        return ret;
    }
}
